﻿using System;
using DAL.File;

namespace DAL.SecureStore
{
    public class LocalUserSecureStore: ISecureStore
    {
        readonly IFileAccess fs = new FileAccess();

        #region ISecureStore Members

        public byte[] GetData(string name)
        {
            var data = fs.ReadFile(GetAutoLoginFilename(name));
            return data == null ? null : ProtectedDataHelper.Unprotect(data);
        }

        public void SetData(string name, byte[] value)
        {
            var fn = GetAutoLoginFilename(name);
            
            if(value == null)
                fs.DeleteFile(fn);
            else
                fs.WriteFile(fn, ProtectedDataHelper.Protect(value));
        }

        #endregion

        protected string GetAutoLoginFilename(string name)
        {
            return Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + @"\" + AppDomain.CurrentDomain.FriendlyName + @"\" + name + ".enc";
        }
    }
}
